﻿using EFCore;
using MyDapper;
using QuickCode.Domain.Entity;
using MySqlSugar;
using MySqlSugar.Context;
using MyFreeSql;
using AutoMapper;
using QuickCode.Application.DTO;
using QuickCode.IApplication;
using MyRedis;
using MyMongoDB;
using System.Linq.Expressions;
using Until.Helper;

namespace QuickCode.Application
{
    public class UserApplication : IUserApplication
    {
        private readonly MyDB myDB;
        private readonly IDapperRepository dapper;
        private readonly ISqlSugarRepository<user_main> sqlSugar;
        public readonly SqlSugarContext sqlSugarContext;
        private readonly IFreesqlRepository<user_main> freeSql;
        private readonly IMapper mapper;
        private readonly IRedisManage _redis;
        //private readonly IMongoDBManage _mongoDb;
        public UserApplication(MyDB mydb, IDapperRepository _dapper, ISqlSugarRepository<user_main> _sqlSugar, SqlSugarContext _sqlSugarContext, IFreesqlRepository<user_main> freesqlRepository,IMapper _mapper, IRedisManage redis)
        {
            myDB = mydb;
            dapper = _dapper;
            sqlSugar = _sqlSugar;
            sqlSugarContext = _sqlSugarContext;
            freeSql = freesqlRepository;
            mapper = _mapper;
            _redis = redis;
            //_mongoDb = mongoDb;
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <returns></returns>
        public string GetFirstUserName()
        {
            #region EF-Core
            //return myDB.Sets<user_main>().Select(l => l.Name).FirstOrDefault() ?? "Nobody" + "Says 'Hello World ！'";
            #endregion

            #region Dapper
            //string sql = "select * from user_main";
            //var users = dapper.QueryList<user_main>(sql)?.Result;
            //if (users != null && users.Any()) return users.ToList()[0].Name;
            //else return "Hello World ！";
            #endregion

            #region SqlSugar
            //string sql = "select * from user_main";
            //var users = sqlSugar.GetListBySql(sql);
            //if (users != null && users.Any()) return users[0].Name ?? "Hello World ！";
            //else return "Hello World ！";
            #endregion

            #region FreeSql
            var users = freeSql.GetList<user_main>();
            if (users != null && users.Any()) return users[0].Name ?? "Hello World ！";
            else return "Hello World ！";
            #endregion
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddUser(UserDTO user)
        {
            user.Id = new user_main().Id;
            user.PassWord = AESHelper.AesEncrypt(user.PassWord, "zhc*zhc*zhc*zhc*zhc*zhc*zhc*zhc*");
            var user1 = mapper.Map<user_main>(user);
            myDB.Add(user1);
            return myDB.SaveChanges(true) > 0;
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <returns></returns>
        public List<UserDTO> GetUsers()
        {
            var users = myDB.Sets<user_main>().ToList();
            var userLst = new List<UserDTO>();
            if (users.Any())
            {
                users.ForEach(user =>
                {
                    userLst.Add(mapper.Map<UserDTO>(user));
                    //_mongoDb.CreateCollection("User", mapper.Map<UserDTO>(user));
                });
            }
            var condition = typeof(UserDTO);
            //var userLstCathe = _mongoDb.GetDocumentList<UserDTO>("User");
            return userLst;
        }

        /// <summary>
        /// 设置用户缓存
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string SetUserCache(string userName)
        {
            _redis.Set("Name", userName, TimeSpan.FromHours(2));
            string get = _redis.GetValue("Name");
            return get;
        }

        /// <summary>
        /// CodeFirst 创建数据库表
        /// </summary>
        /// <returns></returns>
        public bool CreateTable()
        {
            sqlSugarContext.CreateTable();
            return true;
        }

        /// <summary>
        /// DBFirst 创建实体文件
        /// </summary>
        /// <returns></returns>
        public bool CreateEntity()
        {
            string path = System.Environment.CurrentDirectory;
            path = path.Substring(0, path.LastIndexOf(@"\")) + @"\QuickCode.Domain.Entity\SqlSugarModels";
            sqlSugarContext.CreateClassFile(path, "QuickCode.Domain.Entity.SqlSugarModels");
            return true;
        }
    }
}